﻿using System;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Data;
#if SILVERLIGHT
using SelectionChangedEventArgs = Telerik.Windows.Controls.SelectionChangedEventArgs;
#endif
namespace jSLCore.Client.Presentation.TelerikSupport.Controls
{
    public partial class jDateFilteringControl : UserControl, IFilteringControl
    {
        public jDateFilteringControl()
        {
            InitializeComponent();
        }

        private CompositeFilterDescriptor _descriptor;

        private GridViewBoundColumnBase _column;
        public void Prepare(GridViewBoundColumnBase column)
        {
            _column = column;
            datePicker.Focus();
        }

        public void Prepare(Telerik.Windows.Controls.GridViewColumn column)
        {
            if (!(column is GridViewBoundColumnBase)) throw new InvalidOperationException("Column for filtering is not GridViewBoundColumnBase");
            _column = (GridViewBoundColumnBase) column;
            datePicker.Focus();
        }

        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set
            {
                SetValue(IsActiveProperty, value);
            }
        }

        public static DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool),
                                                                                typeof(jDateFilteringControl), null);

        private void date_Changed(object sender, SelectionChangedEventArgs e)
        {
            if(Date==null)
            {
                RemoveFilter();
            }
            else
            {
                Filter();
            }
        }

        private void Filter()
        {
            ClearFilter();

            var memberPath = _column.DataMemberBinding.Path.Path;
            {
                _descriptor = new CompositeFilterDescriptor();
                _descriptor.LogicalOperator = FilterCompositionLogicalOperator.And;
                _descriptor.FilterDescriptors.Add(new FilterDescriptor(memberPath,FilterOperator.IsGreaterThanOrEqualTo, Date));
                _descriptor.FilterDescriptors.Add(new FilterDescriptor(memberPath, FilterOperator.IsLessThanOrEqualTo, Date.Value.AddDays(1)));

                _column.DataControl.FilterDescriptors.Add(_descriptor);
                IsActive = true;
            }
        }

        private DateTime? Date
        {get { return datePicker.SelectedValue; }}

        private void nullale_It(object sender, EventArgs e)
        {
            datePicker.SelectedValue = null;
        }


        private void RemoveFilter()
        {
            ClearFilter();
            IsActive = false;
        }

        private void ClearFilter()
        {
            if (_column.DataControl.FilterDescriptors.Contains(_descriptor))
            {
                _column.DataControl.FilterDescriptors.Remove(_descriptor);
            }
        }
    }
}
